<template>
  <div class="app-container">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="88px">
          <el-form-item label="项目名称" prop="project">
              <el-input v-model="queryParams.project" placeholder="请输入项目名称" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item label="公司/大区/业务单元" prop="danyuan" label-width="148px">
              <el-select v-model="queryParams.danyuan" style="width: 200px;" clearable placeholder="请选择公司/大区/业务单元">
                  <el-option
                      v-for="item in dyList"
                      :key="item.value"
                      :label="item.label"
                      :value="Number(item.value)"
                  ></el-option>
              </el-select>
          </el-form-item>
          <el-form-item label="商务人员" prop="userName">
            <el-select v-model="queryParams.userName" style="width: 200px;" clearable placeholder="请选择商务人员">
                <el-option
                    v-for="item in swList"
                    :key="item.value"
                    :label="item.label"
                    :value="Number(item.value)"
                ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item label="创建时间" prop="dateRange">
              <el-date-picker
              v-model="queryParams.dateRange"
              value-format="yyyy-MM-dd HH:mm:ss"
              type="datetimerange"
              range-separator="-"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
              :default-time="['00:00:00','23:59:59']"
              ></el-date-picker>
          </el-form-item>
          <el-form-item label="告警内容" prop="gjCont">
              <el-input v-model="queryParams.gjCont" placeholder="请输入告警内容" clearable @keyup.enter.native="handleQuery"/>
          </el-form-item>
          <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
              <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
      </el-form>

      <TableCard>
          <el-row :gutter="10" class="mb8">
              <el-col :span="1.5">
                  <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd" >
                      新增
                  </el-button>
              </el-col>
              <el-col :span="1.5">
                  <el-button type="info" plain icon="el-icon-upload2" size="mini" >导入</el-button>
              </el-col>
              <el-col :span="1.5">
                  <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete">
                      删除
                  </el-button>
                </el-col>
              <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
          </el-row>

          <el-table :border="Global.tableShowBorder" v-loading="loading" :data="communicateBookList">
              <el-table-column v-for="(item,index) in tableList" :key="index" :label="item.label" align="center" :prop="item.prop">
                  <template slot-scope="{row}">
                      {{row[item.prop]}}
                  </template>
              </el-table-column>
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="240px">
                  <template slot-scope="scope">
                      <el-button size="mini" type="text" icon="el-icon-view" @click="handleUpdate(scope.row)" >
                          查看
                      </el-button>
                      <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)" >
                          修改
                      </el-button>
                      <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">
                          删除
                      </el-button>
                  </template>
              </el-table-column>
          </el-table>

          <pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList"/>
      </TableCard>

      <!-- 添加或修改个人通讯录对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="1000px" append-to-body>
          <el-form ref="form" :model="form" :rules="rules" label-width="120px">
              <el-row>
                  <el-col :span="12" v-for="(item,index) in tableList" :key="index">
                      <el-form-item :label="item.label" :prop="item.prop">
                          <el-input v-model="form[item.prop]" :placeholder="`请输入${item.label}`"/>
                      </el-form-item>
                  </el-col>
              </el-row>

          </el-form>
          <div slot="footer" class="dialog-footer">
              <el-button type="primary" @click="submitForm">确 定</el-button>
              <el-button @click="cancel">取 消</el-button>
          </div>
      </el-dialog>
  </div>
</template>

<script>
import {
  listCommunicateBook,
  getCommunicateBook,
  delCommunicateBook,
  addCommunicateBook,
  updateCommunicateBook
} from "@/api/common/communicateBook";

export default {
  name: "custoManag",
  data() {
      return {
          swList:[
            {label:'张祁阳',value:1,},
            {label:'王少奇',value:2,},{label:'黄杜娟',value:3,},
          ],
          dyList:[
            {label:'101单元',value:1,},
            {label:'102单元',value:2,},{label:'103单元',value:3,},
          ],
          tableList:[
            {label:'标签',prop:'biaoqian',},{label:'项目序号',prop:'projectNo',},{label:'项目编号',prop:'projectCode',},
            {label:'项目编号',prop:'projectName',},{label:'公司',prop:'gongsi',},{label:'大区',prop:'daqu',},
            {label:'所属业务单元',prop:'danweiName',},{label:'业务类型',prop:'ywType',},{label:'是否购买商业险',prop:'isShangye',},
          ],
          baseUrl: process.env.VUE_APP_BASE_API,
          // 遮罩层
          loading: true,
          // 选中数组
          ids: [],
          // 非单个禁用
          single: true,
          // 非多个禁用
          multiple: true,
          // 显示搜索条件
          showSearch: true,
          // 总条数
          total: 0,
          // 个人通讯录表格数据
          communicateBookList: [],
          // 弹出层标题
          title: "",
          // 是否显示弹出层
          open: false,
          // 查询参数
          queryParams: {
              pageNum: 1,
              pageSize: 20,
              orderByColumn: "createTime",
              isAsc: "descending",
              userId: null,
              avatar: null,
              name: null,
              birthDate: null,
              idNum: null,
              phone: null,
              email: null,
              companyName: null,
              companyAddress: null,
              address: null
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
              name: [
                  {required: true, message: "姓名不能为空", trigger: "blur"}
              ],
          }
      };
  },
  created() {
      this.getList();
  },
  methods: {
      /** 查询个人通讯录列表 */
      getList() {
          this.communicateBookList = [
              {
                biaoqian:'',gongsi:'厦门万云科技有限公司',danweiName:'101单元',projectNo:'',projectCode:'',
                projectName:'金蝶云之家',daqu:'',ywType:'外包',isShangye:'否',
              },
              {
                biaoqian:'',gongsi:'厦门万云科技有限公司',danweiName:'102单元',projectNo:'',projectCode:'',
                projectName:'致远OA',daqu:'',ywType:'外包',isShangye:'否',
              },
              {
                biaoqian:'',gongsi:'厦门万云科技有限公司',danweiName:'101单元',projectNo:'',projectCode:'',
                projectName:'天翎OA',daqu:'',ywType:'外包',isShangye:'否',
              },
              {
                biaoqian:'',gongsi:'厦门万云科技有限公司',danweiName:'102单元',projectNo:'',projectCode:'',
                projectName:'‌华炎魔方',daqu:'',ywType:'外包',isShangye:'否',
              },
              {
                biaoqian:'',gongsi:'厦门万云科技有限公司',danweiName:'103单元',projectNo:'',projectCode:'',
                projectName:'VueOA',daqu:'',ywType:'外包',isShangye:'否',
              },
          ]
          this.loading = false
      },
      // 取消按钮
      cancel() {
          this.open = false;
          this.reset();
      },
      // 表单重置
      reset() {
          this.form = {
              id: null,
              userId: null,
              avatar: null,
              name: null,
              birthDate: null,
              idNum: null,
              phone: null,
              email: null,
              remark: null,
              companyName: null,
              companyAddress: null,
              updateTime: null,
              createTime: null,
              address: null
          };
          this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
          this.queryParams.pageNum = 1;
          this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
          this.resetForm("queryForm");
          this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
          this.ids = selection.map(item => item.id)
          this.single = selection.length !== 1
          this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
          this.reset();
          this.open = true;
          this.title = "添加客户单位管理";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
          this.reset();
          this.form = {...row}
          this.open = true;
          this.title = "修改客户单位管理";
      },
      /** 提交按钮 */
      submitForm() {
          this.$refs["form"].validate(valid => {
              if (valid) {
                  if (this.form.id != null) {
                      updateCommunicateBook(this.form).then(response => {
                          this.$modal.msgSuccess("修改成功");
                          this.open = false;
                          this.getList();
                      });
                  } else {
                      addCommunicateBook(this.form).then(response => {
                          this.$modal.msgSuccess("新增成功");
                          this.open = false;
                          this.getList();
                      });
                  }
              }
          });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
          const ids = row.id || this.ids;
          this.$modal.confirm('是否确认删除该数据项？').then(function () {
              return delCommunicateBook(ids);
          }).then(() => {
              this.getList();
              this.$modal.msgSuccess("删除成功");
          }).catch(() => {
          });
      },
      /** 导出按钮操作 */
      handleExport() {
          this.download('oa/communicateBook/export', {
              ...this.queryParams
          }, `communicateBook_${new Date().getTime()}.xlsx`)
      }
  }
};
</script>
